package com.zxy.salenet.web.service.imple;

import com.zxy.salenet.web.mapper.SalenetGoodsMapper;
import com.zxy.salenet.web.mapper.SalenetOrginMapper;
import com.zxy.salenet.web.mapper.SalenetTypeMapper;
import com.zxy.salenet.web.pojo.SalenetGoods;
import com.zxy.salenet.web.pojo.SalenetOrgin;
import com.zxy.salenet.web.pojo.SalenetType;
import com.zxy.salenet.web.pojo.vo.GoodsTypeVO;
import com.zxy.salenet.web.service.AdminGoodsOperationService;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author: zxy
 * @version: 1.0
 * @description:    管理员商品管理实现接口
 */
 /**
 * AdminGoodsOperationServiceImpl。
 */
@Service
public class AdminGoodsOperationServiceImpl implements AdminGoodsOperationService {
    @Autowired
    private SalenetTypeMapper typeMapper;
    @Autowired
    private SalenetGoodsMapper goodsMapper;

    @Autowired
    private SalenetOrginMapper orginMapper;
    /**
     * 添加商品类别
     * @param salenetType
     * @return
     */
    @Override
    public Integer addGoodsType(SalenetType salenetType) {
        //随机生成tid
        salenetType.setTypeId(new Random().nextInt(100)+10);
        Integer result = typeMapper.addGoodsType(salenetType);
        return result;
    }

    /**
     * 获取所有商品分类
     * @return
     */
    @Override
    public List<GoodsTypeVO> queryAllGoodsType() {
        //视图列表
        List<GoodsTypeVO> goodsTypeVOList = new ArrayList<>();
        //获取所有的商品分类信息
        List<SalenetType> goodsList = typeMapper.selectAllGoods();
        for(SalenetType salenetType : goodsList){
            GoodsTypeVO goodsTypeVO = new GoodsTypeVO();
            //获取父id对应的信息
            if (salenetType.getTypeParentId() != 0){
                SalenetType parent = typeMapper.selectGoodsByTid(salenetType.getTypeParentId());
                goodsTypeVO.setParentGoodsType(parent);
            }else{
                goodsTypeVO.setParentGoodsType(null);
            }
            goodsTypeVO.setChildGoodsType(salenetType);
            goodsTypeVOList.add(goodsTypeVO);
        }
        System.out.println(goodsList);
        return goodsTypeVOList;
    }

    /**
     * 获取所有的父级商品类型
     * @return
     */
    @Override
    public List<SalenetType> queryParentGoodsType() {
        List<SalenetType> typeList = typeMapper.selectParentAll();
        return typeList;
    }

    /**
     * 添加商品品牌
     * @param orgin
     * @return
     */
    @Override
    public Integer addOrgin(SalenetOrgin orgin) {
        //随机生成商品的编号
        orgin.setOrginId(new Random().nextInt(10000)+10+1);
        Integer result = typeMapper.insertOrgin(orgin);
        return result;
    }

    /**
     * 删除商品品牌信息
     * @param orginId
     * @return
     */
    @Override
    public Integer deleteOrginById(Integer orginId) {
        Integer result = typeMapper.deleteOrginById(orginId);
        return result;
    }

    /**
     * 添加商品信息
     * @param goods
     * @return
     */
    @Override
    public Integer addGoods(SalenetGoods goods) {
        System.out.println(goods.toString());
        goods.setGoodsId(new Random().nextInt(10000)+100);
        goods.setGoodsPutaway(new Date());
        goods.setGoodsTimes(new Date());
        goods.setGoodsAuthCode(new Random().nextInt(30000)+102);
        Integer result = typeMapper.addGoods(goods);
        return result;
    }

    /**
     * 修改商品信息
     * @param goods
     * @return
     */
    @Override
    public Integer updateGoods(SalenetGoods goods) {
        System.out.println(goods);
        int result = goodsMapper.updateByPrimaryKeySelective(goods);
        return result;
    }

    /**
     * 将商品设置为下架
     * @param goodsId
     * @return
     */
    @Override
    public Integer deleteGoodsByGoodsId(Integer goodsId) {

        Integer result = goodsMapper.updateGoodsStatusByGoodId(goodsId);

        return result;
    }

    /**
     * 将商品设置为上架
     * @param goodsId
     * @return
     */
    @Override
    public Integer deleteGoodsByGoodsIdUp(Integer goodsId) {
        Integer result = goodsMapper.updateGoodsStatusByGoodIdUp(goodsId);
        return result;
    }

    @Override
    public List<SalenetOrgin> queryAllOrgin() {
        return orginMapper.selectAll();
    }
}
